package com.lgx.mobile.rest.topic;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.common.exception.DataException;
import com.lgx.mobile.entity.Topic;
import com.lgx.mobile.service.ITopicService;

/**
 * 
 * @author xuyichen
 *
 */
@Controller
@RequestMapping("/deleteTopic")
public class DeleteTopicController {
	
	private static final Logger logger = Logger.getLogger(DeleteTopicController.class);

	@Autowired
	ITopicService topicService;
	
	public ITopicService getTopicService() {
		return topicService;
	}
	/**
	 * 话题删除
	 * @param id id is key
	 * @return 0：失败；1：成功；2：话题不存在；3：参数异常，有参数为空
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	@ResponseBody
	public Object getTopic(@PathVariable("id") String id){
		if(StringUtils.isNotBlank(id)){
			Topic topic = getTopicService().findTopicById(Long.parseLong(id));
			if(topic != null){
				String status = "0";
				try {
					getTopicService().deleteTopic(topic);
					status = "1";
				} catch (DataException e) {
					e.printStackTrace();
				}
				return status;
			}else{
				if(logger.isDebugEnabled()){
					logger.debug("deleteTopic faild id is error");
				}
//				"deleteTopic faild please check topic is exists";
				return "2";
			}
		}else{
			if(logger.isDebugEnabled()){
				logger.debug("deleteTopic faild id is not filled");
			}
//		    "deleteTopic faild please check id is filled";
			return "3";
		}
	}
}
